package cn.nawang.ebeim.test.service;

import cn.nawang.ebeim.test.db.DBHelper;
import cn.nawang.ebeim.test.domain.FileInfo;
import cn.nawang.ebeim.test.queue.Queues;
import cn.nawang.ebeim.test.task.DownloadTask;

import java.io.File;
import java.util.List;

/**
 * Created by GanJc on 2015-12-08 15:45
 */
public class DownloadService {

    private DBHelper dbHelper ;

    public DownloadService() {
        dbHelper = new DBHelper();
    }

    public void download(){
        List<FileInfo> files = dbHelper.findFiles();
        TestData.setTotalTransferFile(files.size());
        for (FileInfo fileInfo : files) {
            System.out.println(fileInfo);
            cn.nawang.ebeim.test.transfer.FileInfo info = new cn.nawang.ebeim.test.transfer.FileInfo();
            info.setDataId(fileInfo.getContainer());
            info.setSignature(fileInfo.getSignature());
            String path =  fileInfo.getAbsPath() + File.separator + fileInfo.getFilePath() ;
            info.setPath(path);
            info.setType(cn.nawang.ebeim.test.transfer.FileInfo.TYPE_FILE_DOWNLOAD);
            try {
                Queues.downloadTaskQueue.put(new DownloadTask(info));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
